#!/usr/bin/env bash

set -uo pipefail;

####################################
# Ensure we can execute standalone #
####################################

function early_death() {
  echo "[FATAL] ${0}: ${1}" >&2;
  exit 1;
};

if [ -z "${TFENV_ROOT:-""}" ]; then
  # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
  readlink_f() {
    local target_file="${1}";
    local file_name;

    while [ "${target_file}" != "" ]; do
      cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
      file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
      target_file="$(readlink "${file_name}")";
    done;

    echo "$(pwd -P)/${file_name}";
  };

  TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
  [ -n "${TFENV_ROOT}" ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
else
  TFENV_ROOT="${TFENV_ROOT%/}";
fi;
export TFENV_ROOT;

if [ -n "${TFENV_HELPERS:-""}" ]; then
  log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
else
  [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
  if source "${TFENV_ROOT}/lib/helpers.sh"; then
    log 'debug' 'Helpers sourced successfully';
  else
    early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh";
  fi;
fi;

# Ensure libexec and bin are in $PATH
for dir in libexec bin; do
  case ":${PATH}:" in
    *:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";;
    *)
      log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now";
      export PATH="${TFENV_ROOT}/${dir}:${PATH}";
      ;;
  esac;
done;

#####################
# Begin Script Body #
#####################

[ "${#}" -ne 0 ] \
  && log 'error' "usage: tfenv list";

[ -d "${TFENV_CONFIG_DIR}/versions" ] \
  || log 'error' 'No versions available. Please install one with: tfenv install';

[[ -x "${TFENV_CONFIG_DIR}/versions" && -r "${TFENV_CONFIG_DIR}/versions" ]] \
  || log 'error' "tfenv versions directory is inaccessible: ${TFENV_CONFIG_DIR}/versions";

version_name="$(tfenv-version-name 2>/dev/null || true)" \
  && log 'debug' "tfenv-version-name reported: ${version_name}";
export version_name;

if [ -z "${TFENV_TERRAFORM_VERSION:-""}" ]; then
  version_source="$(tfenv-version-file)" \
    && log 'debug' "tfenv-version-file reported: ${version_source}" \
    || log 'error' "tfenv-version-file failed";
else
  version_source='TFENV_TERRAFORM_VERSION';
fi;
export version_source;

# Register for whether a default terraform version has yet been set
declare -i default_set=0;

print_version () {
  if [ "${1}" == "${version_name}" ]; then
    echo "* ${1} (set by ${version_source})";
    default_set=1;
  else
    echo "  ${1}";
  fi;
};

log 'debug' 'Listing versions...';
local_versions=($(\find "${TFENV_CONFIG_DIR}/versions/" -type d -exec basename {} \; \
  | tail -n +2 \
  | sort -t'.' -k 1nr,1 -k 2nr,2 -k 3nr,3));

log 'debug' "Local versions: ${local_versions[@]}";

log 'debug' 'Printing versions...';
for local_version in ${local_versions[@]}; do
  print_version "${local_version}";
done;

[ "${default_set}" -eq 0 ] && log 'info' "No default set. Set with 'tfenv use <version>'";

exit 0;
